<?php
/**
 * 
 * @author chenqi
 * @uses 申请小鸽
 * @date 2015-07-10
 *
 */
class System_money extends WX_Controller{
   public function __construct()
	{
		parent::__construct();
		$this->load->model('system_money_log_model');
		
	}
        /**
         * 提现列表
         */
        public function  index(){
            $system_money_log_list=$this->system_money_log_model->getSystemMoneyLogAll(array('user_id'=>$this->user['id']));
            $this->load->model('user_model');
            $users=array();
            $result=array();
            foreach($system_money_log_list as $key=>$value){
                $shenhe_url='';
                switch ($value['status']){
                    case '0': 
                        $value['status_text']='已提交';
                        break;
                    case '1': 
                        $value['status_text']='已拒绝';
                        break;
                    case '2': 
                        $value['status_text']='已完成';
                        break;
                    case '3': 
                        $value['status_text']='已取消';
                        break;
                }
                if(isset($users[$value['user_id']])){
                    $value['true_name']=$users[$value['user_id']]['true_name'];
                    $value['username']=$users[$value['user_id']]['username'];
                }
                $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
                $value['last_time']=date('Y-m-d H:i:s',$value['last_time']);
                $user_info=$this->user_model->checkUser(array('id'=>$value['user_id']),'username,true_name,tixian');
                if($user_info){
                    $value['true_name']=$user_info['true_name'];
                    $value['username']=$user_info['username'];
                    $users[$value['user_id']]['true_name']=$user_info['true_name'];
                    $users[$value['user_id']]['username']=$user_info['username'];
                }
                $value['operate']=$shenhe_url;
                $result[]=$value;
            }
            $this->data['site_title']='报单币记录';
            $this->data['system']=$result;
            $this->load->view('/wap/historical_record',$this->data);
        }
        
        public function add_page() {
            $this->data['site_title']='转报单币';
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('user.id'=>$this->user['id']));
            $this->data['user']=$user;
            
            $this->load->view('/wap/want_record',$this->data);
        }
         /**
     * 验证二级密码
     */
    public function check_password(){
        $message=false;
        if(empty($_POST['username']) || empty($_POST['password'])){
            $message=false;
            
        }else{
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']),'username,password2');
            if(empty($user)){
                $message=false;
            }else{
                if($user['password2']==md5($_POST['password'])){
                    $message=true;
                }
            }
        }
        echo json_encode($message);
    }
     /**
     * 添加申请提现记录
     */
    public function add(){
        $message=false;
        $is_success=true;
        if(empty($_POST['username']) || empty($_POST['password'])||empty($_POST['money'])){
            $message='参数错误';
            $is_success=false;
        }else{
            $this->load->model('user_model');
            $user=$this->user_model->checkUser(array('username'=>$_POST['username']),'id,username,password2,true_name,money,freeze,balance,system_money,system_balance');
            if(empty($user)){
                $message='用户不存在';
                $is_success=false;
            }else if(($user['balance']-$_POST['money'])<0){
				$message='余额不足';
                $is_success=false;
			}else{
                if($user['password2']==md5($_POST['password'])){
                    //$tax=$_POST['money']*0.08;
                    $feiyong=$_POST['money']*ZHUANBIN;
                    $data['user_id']=$user['id'];
                    $data['money']  =$_POST['money'];
                    $data['freeze']  =$_POST['money'];
                    $data['feiyong'] =$feiyong;
                    $data['shiji_money']=$_POST['money']-$feiyong;
                    $data['status'] =0;
                    $data['create_time']=time();
                    $data['last_time']  =time();
                    $data['operation']  =$this->user['true_name'];
                    $data['operation']  =$this->user['username'];
                    
                    $data['balance']    =$user['balance']-$_POST['money'];
                    
                    $result=$this->system_money_log_model->addSystemMoneyLog($data);
                    if($result){
                        $update_data['balance']=$data['balance'];
                        $update_data['freeze']=$user['freeze']+$_POST['money'];
                        $update_data['system_money']=$user['system_money']+$data['shiji_money'];
                        $update_data['system_balance']=$user['system_balance']+$data['shiji_money'];
                        $update=$this->user_model->editUser(array('id'=>$user['id']),$update_data);
                        $update=true;
                        if($update){
                            $logdata[]=array(
                                'user_id'=>$user['id'],
                                'username'=>$user['username'],
                                'true_name'=>$user['true_name'],
                                'money'=>$data['shiji_money'],
                                'type' =>'sysytem_zhuan',
                                'remark'=>$user['true_name'].'您于'.date('Y-m-d H:i:s',time())."申请转报单币".$_POST['money']."元,扣除费用".$feiyong.'实际到账'.$data['shiji_money'],
                                'create_time'=>  time()
                            );
                            $this->add_money_log($logdata);
                        }else{
                            $this->system_money_log_model->deleteSystemMoneyLog(array('id'=>$result));
                            $message='添加失败';
                        }
                    }
                }else{
                    $is_success=false;
                    $message='二级密码错误';
                }
            }
        }
        echo json_encode(array('success'=>$is_success,'msg'=>$message));
    }

        
	//获取城市联动
	private function city($parentno=0,$arealevel=1){

		$where["parentno"] = $parentno;
		$where["arealevel"] = $arealevel;
		
		$count=$this->area_model->getCount($where);

		if($count)
			$province = $this->area_model->getArea($where,$count,0,"sort ASC");
		
		return $province;
	}
	//城市联动接口
	public function area(){
		$parentno = $this->input->get('parentno');
		$arealevel= $this->input->get('arealevel');
		$prov=$this->city($parentno,$arealevel);
		echo json_encode($prov);
	}
	
	//提交申请
	public function submit(){
		$userid = $this->user['id'];
		//判断是否已申请		
		$name = $this->input->get('name');
		$phone = $this->input->get('phone');
		$identy = $this->input->get('identy');
		$area = $this->input->get('area');
		$area_no = $this->input->get("area_no");//地区编码
		$service = $this->input->get('service');
		$service_value = $this->input->get('service_value');
		$service_id = $this->input->get('service_id');
		$ss = array();
		foreach ($service_id as $k=>$v){
			$ss[$v] = $service_value[$k];
		}
		$ss = serialize($ss);
		
		$areas["detail"] = $area;
		$areas["area_no"] = $area_no;
		
		$data = array(
				"user_id"=>$userid,
				"server_card"=>$identy,
				"name"=>$name,
				"phone"=>$phone,
				"classes"=>$ss,
				"areas"=>serialize($areas),
				"server_flag"=>1,
				"check_flag"=>2,
				"update_time"=>$this->current_time,
				"create_time"=>$this->current_time
				);
		
		$this->load->model('master_model');
		
		$res = $this->master_model->addMaster($data);
		if($res){
			$this->response(array('status'=>true, 'data'=>'申请成功！'), 200);
		}
		$this->response(array('status'=>false, 'data'=>'预约出错，请重试！'));
		
	}
 
 	

}